import React, { Component } from 'react';
import { Tree, Table } from 'antd';

const { TreeNode } = Tree;

class JurTreeConComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      treeData: props.treeData || [],
      gdsformconst: props.gdsformconst, /* 常量 */
      jurConColumn: [],
      jurConData: [],
    };
  }

  componentWillMount() {
    /* 获取表数据 */
    const { gdsformconst } = this.state;
    if (this.state.jurConColumn.length === 0) {
      /* 权限内容标题 */
      const column = gdsformconst.filter(item => item.sName === 'jurUserGroupData')[0];
      const jurConColumn = [{
        title: column.showName,
        dataIndex: column.sName,
        key: column.sName,
      }];
      this.setState({ jurConColumn });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { treeData, checkedHaveKey } = nextProps;
    if (this.state.treeData.length === 0) {
      this.setState({ treeData });
    }
    if (treeData.length > 0 && checkedHaveKey.length >= 0) {
      const newTreeData = [];
      treeData.forEach((child) => {
        const index = checkedHaveKey.findIndex(item => item === child.key);
        if (index >= 0) {
          newTreeData.push({
            title: child.title,
            key: child.key,
            disabled: true,
          });
          if (child.children !== undefined && child.children.length > 0) {
            const childIndex = newTreeData.findIndex(item => item.key === child.key);
            this.handleChildrenData(child.children, newTreeData[childIndex], checkedHaveKey);
          }
        }
      });
      const jurConData = [{
        key: 1,
        jurUserGroupData:
  <Tree
    checkable
    checkedKeys={checkedHaveKey}
  >
    {this.renderTreeNodes(newTreeData)}
  </Tree>,
      }];
      this.setState({ jurConData });
    }
  }

  handleChildrenData = (data, treeChildData, checkedHaveKey) => {
    treeChildData.children = [];
    data.forEach((child) => {
      const index = checkedHaveKey.findIndex(item => item === child.key);
      if (index >= 0) {
        treeChildData.children.push({
          title: child.title,
          key: child.key,
          disabled: true,
        });
        if (child.children !== undefined && child.children.length > 0) {
          const childIndex = treeChildData.children.findIndex(item => item.key === child.key);
          this.handleChildrenData(child.children, treeChildData.children[childIndex], checkedHaveKey);
        }
      }
    });
  };

  renderTreeNodes = (data) => {
    return data.map((item) => {
      if (item.children) {
        return (
          <TreeNode title={item.title} key={item.key} disabled={item.disabled} dataRef={item}>
            {this.renderTreeNodes(item.children)}
          </TreeNode>
        );
      }
      return <TreeNode {...item} />;
    });
  };

  render() {
    const { jurConColumn, jurConData } = this.state;
    return (
      <Table
        bordered
        columns={jurConColumn}
        dataSource={jurConData}
        size="middle"
        pagination={false}
      />
    );
  }
}

export default JurTreeConComponent;
